tool
extends VBoxContainer

var datas_path  ="res://addons/myAdd/datas/"
var pageTSCN:PackedScene = load("res://addons/myAdd/plug_UI/Page/Page.tscn")


var pages = [
	["Control节点","control.json",false],
	["2D节点","2D.json",true],
	["3D节点","3D.json",true],
	["其他节点","others.json",true],
]

func _enter_tree():
	# 解决移动dock位置重复加载bug
	# 解决设计状态下_enter_tree后加载控件
	var mtab = $"tab" # 页面选项卡
	if mtab.get_child_count() == 0 and get_parent() is TabContainer: 
		for page in pages:
			loadPage(page[0],page[1],page[2])
		pass
		
		
# 加载页面 - 添加选项卡控件
func loadPage(title:String,fName:String,is_lazy_load:= false) -> void:
	var mtab = $"tab" # 页面选项卡
	var page = pageTSCN.instance()
	var file = myFile.new()
	page.nodes = file.loadJSON(datas_path + fName) # 节点数据
	page.title = title # 标题
	page.is_lazy_load = is_lazy_load # 是否懒加载
	mtab.add_child(page)
	page.owner = mtab

# 切换语言
func _on_langSwitch_toggled(button_pressed):
	var mtab = $"tab" # 页面选项卡
	var page_ctrls = mtab.get_children()
	for page_ctl in page_ctrls:
		page_ctl.is_en = button_pressed
	pass 

# 页面选项卡被选中
func _on_tab_tab_selected(tab):
	var mtab = $"tab" # 页面选项卡
	var page = mtab.get_child(tab)
	if page.is_lazy_load:
		page.load_page()
		page.is_lazy_load = false
	pass


func _on_MenuButton_pressed():
	var menuAbout = $topToolbar/MenuButton
	var popMenu:PopupMenu = menuAbout.get_popup()
	popMenu.connect("index_pressed",self,"menuAbout_pressed")
	pass 

func menuAbout_pressed(index):
	var tscn = load("res://addons/myAdd/plug_UI/dlg/aboutDlg.tscn")
	match index:
		0:
			var dlg = myDialog.new()
			dlg.popupWithPackedScene(tscn,$".")
	pass


func _on_ToolButton_toggled(button_pressed):
	var img_toolbarview = load("res://addons/myAdd/imgs/icon1.png")
	var img_normalview = load("res://addons/myAdd/imgs/icon2.png")
	# 更换按钮图标
	if button_pressed:
		$topToolbar/ToolButton.icon = img_normalview
		$topToolbar/ToolButton.hint_tooltip = "切换到普通模式"
	else:
		$topToolbar/ToolButton.icon = img_toolbarview
		$topToolbar/ToolButton.hint_tooltip = "切换到ToolBar模式"
	# 修改每个页面的is_toolbarview属性
	var mtab = $"tab" # 页面选项卡
	var page_ctrls = mtab.get_children()
	for page_ctl in page_ctrls:
		page_ctl.is_toolbarview = button_pressed
	pass 

